package com.hnas.eCard.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hnas.account.dao.AccountDAO;
import com.hnas.api.dto.DTOResult;
import com.hnas.common.util.PostConnServerUtils;
import com.hnas.common.util.ResultCode;
import com.hnas.eCard.dao.AccECardDAO;
import com.hnas.eCard.dao.CardTrailDAO;
import com.hnas.eCard.dao.ECardShareCodeDao;
import com.hnas.eCard.domain.CardTrailStatus;
import com.hnas.eCard.domain.ECardCommonFinalValue;
import com.hnas.eCard.domain.ECardStatus;
import com.hnas.eCard.domain.ReturnECardMapper;
import com.hnas.eCard.orm.AccECard;
import com.hnas.eCard.orm.CardTrail;
import com.hnas.eCard.service.CardTrailService;
import com.hnas.eCard.service.dto.DTOSaleCardOrderDetail;

@Component("cardTrailService")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class CardTrailServiceImpl implements CardTrailService {

	@Autowired
	private CardTrailDAO cardTrailDAO;
	@Autowired
	private ECardShareCodeDao eCardShareCodeDao;
	@Autowired
	private AccountDAO accountDAO;
	@Autowired
	private AccECardDAO accECardDAO;
	
	@Value("#{component['subCode']}") // 发卡类型
	private String subCode;
	
	@Autowired
	private PostConnServerUtils postConnServerUtils;
	@Override
	public JSONObject findECardsByStatus(String accId, String phoneNo, String searchStatus, Integer pageNo, Integer pageSize) throws Exception {
		JSONObject result = new JSONObject();
		List<CardTrail> list = cardTrailDAO.findCardTrailByStatus(accId, phoneNo, searchStatus, pageNo, pageSize);
		if(null != list && list.size() > 0){
			JSONArray array = new JSONArray();
			JSONObject temp = null;
//			GetBalanceForm getBalanceForm = null;
			for(CardTrail eCard : list){
				temp = new JSONObject();
//				getBalanceForm = new GetBalanceForm();
//				getBalanceForm.setCardNo(eCard.getSendCardNo());
//				getBalanceForm.setSubCode(subCode);
				temp.put(ECardCommonFinalValue.card_no, eCard.getSendCardNo());
				temp.put(ECardCommonFinalValue.card_mark, "");
				temp.put(ECardCommonFinalValue.status, ReturnECardMapper.getReturnECardStatus(searchStatus, eCard.getStatus()));
				temp.put(ECardCommonFinalValue.face_value, "");
				temp.put(ECardCommonFinalValue.expiry_date, "");
				temp.put(ECardCommonFinalValue.send_phone_no, eCard.getSendPhoneNo());
				temp.put(ECardCommonFinalValue.send_name, eCard.getSendName());
				temp.put(ECardCommonFinalValue.send_msg, eCard.getSendMsg());
				temp.put(ECardCommonFinalValue.receive_phone_no, eCard.getReceivePhoneNo());
				temp.put(ECardCommonFinalValue.receive_name, eCard.getReceiveName());
				temp.put(ECardCommonFinalValue.remark, eCard.getRemark());
//				temp.put(ECardCommonFinalValue.balance, postConnServerUtils.getBalance(getBalanceForm));
				temp.put(ECardCommonFinalValue.balance, eCard.getCurrentBalance());
				temp.put(ECardCommonFinalValue.create_type, eCard.getCreateType());
				array.add(temp);
			}
			result.put(ECardCommonFinalValue.ecard_list, array);
		}
		return result;
	}
	
	@Override
	public CardTrail saveOrUpdate(CardTrail vo) throws Exception {
		return cardTrailDAO.saveOrUpdate(vo);
	}

	@Override
	public List<CardTrail> findCardsByStatus(String accId, String phoneNo,String status) {
		return cardTrailDAO.findCardsByStatus(accId, phoneNo, status);
	}

	@Override
	public List<CardTrail> listByCardAndDate(String cardNo, String date) {
		return cardTrailDAO.listByCardAndDate(cardNo,date);
	}

	@Override
	public List<CardTrail> listByCards(String sendAccId, String sendPhoneNo, String receivePhoneNo,
			String changeString) {
		return cardTrailDAO.listByCards(sendAccId, sendPhoneNo, receivePhoneNo,changeString);
	}

	@Override
	public int batchSave(String sendAccId, DTOSaleCardOrderDetail orderDetailDto) {
		return cardTrailDAO.batchSave(sendAccId, orderDetailDto);
	}

	@Override
	public boolean shareECard(AccECard accECard,CardTrail cardTrail) throws Exception{
		CardTrail cardTrailResult = this.cardTrailDAO.saveOrUpdate(cardTrail);
		//保存卡转赠轨迹
		//卡片状态要改成赠送中
		accECard.setStatus(ECardStatus.GIVING);
		AccECard accECardResult = this.accECardDAO.saveOrUpdate(accECard);
		return (cardTrailResult!=null && accECardResult!=null);
	}

	@Override
	public DTOResult findCardInfo(String shareCode,DTOResult dtoResult) throws Exception{
		CardTrail cardTrail = this.cardTrailDAO.findByShareCode(shareCode);
		if(cardTrail == null) {//没有赠卡记录
			dtoResult.setResultCode(ResultCode.ERROR_226);
			dtoResult.setResultData(null);
			dtoResult.setResultMsg(ResultCode.getResultValue(ResultCode.ERROR_226));
			return dtoResult;
		}
		String status = cardTrail.getStatus();
		JSONObject ret = new JSONObject();
		if(CardTrailStatus.CONFIRM_RECEIVE_GIVE.equals(status)) {
			ret.put(ECardCommonFinalValue.card_no, cardTrail.getSendCardNo());
			ret.put(ECardCommonFinalValue.denomination, cardTrail.getCurrentBalance());
			ret.put(ECardCommonFinalValue.ecard_status, "1");
			ret.put(ECardCommonFinalValue.send_phone_no, cardTrail.getSendPhoneNo());
			ret.put(ECardCommonFinalValue.send_wx_image, cardTrail.getSendWxImage());
			ret.put(ECardCommonFinalValue.send_wx_name, cardTrail.getSendWxName());
			ret.put(ECardCommonFinalValue.receive_phone_no, cardTrail.getReceivePhoneNo());
			ret.put(ECardCommonFinalValue.receive_wx_image, cardTrail.getReceiveWxImage());
			ret.put(ECardCommonFinalValue.receive_wx_name, cardTrail.getReceiveWxName());
			ret.put(ECardCommonFinalValue.send_msg, cardTrail.getSendMsg());
			ret.put(ECardCommonFinalValue.receive_time, cardTrail.getCreateTime());
		}else if(CardTrailStatus.RECEIVING_GIVING.equals(status)) {
			ret.put(ECardCommonFinalValue.ecard_status, "0");
			ret.put(ECardCommonFinalValue.card_no, cardTrail.getSendCardNo());
			ret.put(ECardCommonFinalValue.denomination, cardTrail.getCurrentBalance());
			ret.put(ECardCommonFinalValue.send_phone_no, cardTrail.getSendPhoneNo());
			ret.put(ECardCommonFinalValue.send_wx_image, cardTrail.getSendWxImage());
			ret.put(ECardCommonFinalValue.send_wx_name, cardTrail.getSendWxName());
			ret.put(ECardCommonFinalValue.receive_phone_no, cardTrail.getReceivePhoneNo());
			ret.put(ECardCommonFinalValue.receive_wx_image, cardTrail.getReceiveWxImage());
			ret.put(ECardCommonFinalValue.receive_wx_name, cardTrail.getReceiveWxName());
			ret.put(ECardCommonFinalValue.send_msg, cardTrail.getSendMsg());
			ret.put(ECardCommonFinalValue.receive_time, cardTrail.getCreateTime());
		}
		//TODO 如果退卡，要增加已退卡的返回
		dtoResult.setResultCode(ResultCode.SUCCESS);
		dtoResult.setResultData(JSON.toJSONString(ret));
		dtoResult.setResultMsg(ResultCode.getResultValue(ResultCode.SUCCESS));
		return dtoResult;
	}

	@Override
	public CardTrail findByShareCode(String shareCode) {
		return this.cardTrailDAO.findByShareCode(shareCode);
	}
}
